import { forceLogout, listOnline } from '@/api/monitor/online'
import { formatDate } from '@/utils/common'
import { message } from '@/utils/message'
import type { PaginationProps } from '@pureadmin/table'
import { ElMessageBox } from 'element-plus'
import { onMounted, reactive, ref, toRefs } from 'vue'

export function useOnline() {
  const data = reactive({
    form: {},
    queryParams: {
      ipaddr: undefined,
      userName: undefined,
    },
  })
  const { form, queryParams } = toRefs(data)
  const onlineId = ref(0)
  const title = ref('')
  const ids = ref([])
  const single = ref(true)
  const multiple = ref(true)
  const dataList = ref([])
  const loading = ref(true)
  const showSelection = true
  const formDialogVisible = ref(false)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
  })
  const columns: TableColumnList = [
    {
      type: 'selection',
      width: 55,
      align: 'left',
      hide: ({ checkList }) => !checkList.includes('勾选列'),
    },
    {
      label: '序号',
      type: 'index',
      width: 70,
      hide: ({ checkList }) => !checkList.includes('序号列'),
    },
    {
      label: '会话编号',
      prop: 'tokenId',
      minWidth: 120,
    },
    {
      label: '登录名称',
      prop: 'userName',
      minWidth: 120,
    },
    {
      label: '主机',
      prop: 'ipaddr',
      minWidth: 120,
    },
    {
      label: '登录地点',
      prop: 'loginLocation',
      minWidth: 120,
    },
    {
      label: '浏览器',
      prop: 'browser',
      minWidth: 120,
    },
    {
      label: '操作系统',
      prop: 'os',
      minWidth: 120,
    },
    {
      label: '登录时间',
      minWidth: 180,
      prop: 'loginTime',
      formatter: ({ createTime }) => formatDate(createTime),
    },
    {
      label: '操作',
      fixed: 'right',
      width: 180,
      slot: 'operation',
    },
  ]

  // /** 重置新增的表单  */
  // function reset() {
  //   form.value = {};
  // }

  /** 强退按钮操作 */
  function handleForceLogout(row) {
    ElMessageBox.confirm('是否确认强退名称为"' + row.userName + '"的用户？')
      .then(function () {
        return forceLogout(row.tokenId)
      })
      .then(() => {
        onSearch()
        message('强退成功', { type: 'success' })
      })
  }

  /** 改变分页大小 */
  function handleSizeChange(val: number) {
    pagination.pageSize = val
    onSearch()
  }

  /** 改变当前分页 */
  function handleCurrentChange(val: number) {
    pagination.currentPage = val
    onSearch()
  }

  /** 改变多选项 */
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.Id)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }

  /** 搜索按钮操作 */
  async function onSearch() {
    loading.value = true
    const params = {
      ...queryParams.value,
      pageNum: pagination.currentPage,
      pageSize: pagination.pageSize,
    }
    const { rows, total } = await listOnline(params)
    const start = (pagination.currentPage - 1) * pagination.pageSize
    const end = start + pagination.pageSize
    const data = rows.slice(start, end)
    dataList.value = data
    pagination.total = total
    loading.value = false
  }

  /** 重置按钮操作 */
  const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
    onSearch()
  }

  onMounted(() => {
    onSearch()
  })

  return {
    form,
    title,
    onlineId,
    loading,
    columns,
    multiple,
    dataList,
    pagination,
    queryParams,
    showSelection,
    formDialogVisible,
    onSearch,
    resetForm,
    handleForceLogout,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
  }
}
